package com.ayoka.parenting.ui.group.topic.publish;

import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.PublishTopicBean;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.ayoka.parenting.widget.PickItemDialog;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.utils.UIUtils;
import com.jincp.filepicker.bean.BaseFile;
import com.jincp.filepicker.ui.image.ImagePickerManager;
import com.jincp.image.compress.ImageCompress;
import com.jincp.image.compress.ImageCompressListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: PublishTopicPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class PublishTopicPresenter extends BasePresenterImpl<PublishTopicContract.View> implements PublishTopicContract.Presenter{

    private PublishTopicModel model;

     public PublishTopicPresenter() {
        model = new PublishTopicModel();
     }


    /**
     * @param fragmentManager
     * @Title: PicDialog
     * @Description: 是从相册选择图片，还是拍照更改图片
     */
    public void showPickPicturePopup(FragmentManager fragmentManager) {
        PickItemDialog dialog = new PickItemDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .build();
        dialog.setPickItemList(PickItemDialog.getPickPhotoItemList());
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialog, Object result) {
                dialog.dismiss();
                PickItemDialog.PickItem pickItem = (PickItemDialog.PickItem) result;
                mView.get().onPickPhotoItemClick((int) pickItem.getItemValue());
            }
        });
        dialog.show(fragmentManager, "PickItemDialog");
    }

    /**
     * 发表topic
     * @param circleGroupId
     * @param content
     * @param file
     */
    public void publishTopic(String circleGroupId, String content, File file) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.publishTopic(this, circleGroupId, content, file, new SimpleRequestCallback<PublishTopicBean>() {
            @Override
            public void onSuccess(PublishTopicBean response) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onPublishTopicCallback(true, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errorMessage);
                mView.get().onPublishTopicCallback(false, null);
            }
        });
    }

    // 设置图片到list
    public void configFileList(List<BaseFile> selectPictureList) {
        if (selectPictureList == null || selectPictureList.isEmpty()) return;
        List<String> needCompressImagePathList = new ArrayList<>();
        for (BaseFile imageFile : selectPictureList) {
            needCompressImagePathList.add(imageFile.getPath());
        }
        ImageCompress.getInstance().compressImage(needCompressImagePathList, 80, new ImageCompressListener() {
            @Override
            public void onCompressCompleted(List<String> compressedImagePathList) {
                if (compressedImagePathList == null || compressedImagePathList.isEmpty())
                    return;
                mView.get().configCompressList(true, compressedImagePathList);
            }
        });
    }
}
